package com.henghai.zhenxuan.ui.page.mine;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.bean.UserPointsLogBean;
import com.henghai.zhenxuan.domain.request.UserPointsLogListRequester;
import com.henghai.zhenxuan.ui.page.adapter.IntegralListAdapter;
import com.google.android.material.tabs.TabLayout;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

public class IntegralDetailFragment extends BaseFragment {

    private IntegralDetailStates mStates;
    private IntegralListAdapter mAdapter;
    private UserPointsLogListRequester mUserPointsLogListRequester;
    private RefreshLayout mRefreshLayout;
    private int current = 0;

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(IntegralDetailStates.class);
        mUserPointsLogListRequester = getFragmentScopeViewModel(UserPointsLogListRequester.class);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUserPointsLogListRequester.getResult().observe(this, httpBeanDataResult -> {
            if (mRefreshLayout != null) {
                mRefreshLayout.finishRefresh();
            }
            if (!httpBeanDataResult.getResponseStatus().isSuccess()) {
                TipDialog.show(httpBeanDataResult.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }
            mStates.list.set(httpBeanDataResult.getResult().getData().getRows());
        });
        mUserPointsLogListRequester.request(this, "");
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new IntegralListAdapter(getContext());
        return new DataBindingConfig(com.henghai.zhenxuan.R.layout.fragment_integral_detail, BR.vm, mStates).addBindingParam(BR.listenerTab, new TabListenerHandler()).addBindingParam(BR.adapter, mAdapter).addBindingParam(BR.listener, new ListenerHandler()).addBindingParam(BR.listenerRefresh, new ListenerRefreshHandler());
    }


    public class TabListenerHandler implements TabLayout.OnTabSelectedListener {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            current = tab.getPosition();
            mUserPointsLogListRequester.request(getViewLifecycleOwner(), current == 0 ? "" : current + "");
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    }

    public class ListenerRefreshHandler implements OnRefreshListener {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            mRefreshLayout = refreshLayout;
            mUserPointsLogListRequester.request(getViewLifecycleOwner(), current == 0 ? "" : current + "");
        }
    }

    public class ListenerHandler implements OnTitleBarListener {
        @Override
        public void onLeftClick(TitleBar titleBar) {
            OnTitleBarListener.super.onLeftClick(titleBar);
            nav().navigateUp();
        }

        @Override
        public void onRightClick(TitleBar titleBar) {
            OnTitleBarListener.super.onRightClick(titleBar);
        }
    }

    public static class IntegralDetailStates extends StateHolder {
        public final State<ArrayList<UserPointsLogBean.RowsBean>> list = new State<>(new ArrayList<>());
    }
}
